<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/21
 * Time: 13:55
 */

namespace Admin\Controller;


use Think\Controller;

class GdzcController extends AdminController
{
    //对父类构造函数的补充
    public function _myinitialize()
    {
        $this->db = M('gdzc');
    }

    public function lst()
    {
        $session_where=I('session_where');//不为空，则表明是从add/edit/del中跳转过来的，刚查询条件从session中取。
        if(empty($session_where)){
            //查询条件
            $where['status'] = array('gt', 0);

            //所有类别列表
            $lx_arr = $this->db->where($where)->getField('lx', true);
            $lx_arr = array_values(array_unique($lx_arr));
            $this->assign('lx_arr', $lx_arr);

            //所有使用状态列表
            $syzt_arr = $this->db->where($where)->getField('syzt', true);
            $syzt_arr = array_values(array_unique($syzt_arr));
            $this->assign('syzt_arr', $syzt_arr);

            //所有存放地点列表
            $cfdd_arr = $this->db->where($where)->getField('cfdd', true);
            $cfdd_arr = array_values(array_unique($cfdd_arr));
            $this->assign('cfdd_arr', $cfdd_arr);

            $lx=I('lx');
            $syzt=I('syzt');
            $cfdd=I('cfdd');
            if(!empty($lx)) $where['lx']=$lx;
            if(!empty($syzt)) $where['syzt']=$syzt;
            if(!empty($cfdd)) $where['cfdd']=$cfdd;

            //将查询条件存入session中，方便更新时跳转
            session(CONTROLLER_NAME."where",$where);
        }else{
            $where=session(CONTROLLER_NAME."where");
        }

        //列表数据
        $lsts = $this->db->where($where)->order("updatetime desc")->select();
        $this->assign('lsts', $lsts);//列表数据

        $this->display();
    }

    //新增 www.glxt.com/admin/khzy/add
    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime']=time();
                $data['status'] = 1;
                $data['je']=$data['sl']*$data['dj'];
                $data['jcz']=$data['je']*$data['czl'];//净残值=金额*残值率
                $data['yzjl']=round(($data['czl']+1)/$data['synx']/12*100,2)."%";//月折旧率=（1＋残值率）÷使用年限÷12*100％
                $data['yzje']=($data['je']-$data['jcz'])*$data['yzjl'];//月折旧额=（金额-净残值）*月折旧率

            }
            $rid = $this->db->data($data)->add();

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }

    //修改
    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime']=time();
                $data['status'] = 1;
                $data['je']=$data['sl']*$data['dj'];
                $data['jcz']=$data['je']*$data['czl'];//净残值=金额*残值率
                $data['yzjl']=round(($data['czl']+1)/$data['synx']/12*100,2)."%";//月折旧率=（1＋残值率）÷使用年限÷12*100％
                $data['yzje']=($data['je']-$data['jcz'])*$data['yzjl'];//月折旧额=（金额-净残值）*月折旧率
            }
            $rid = $this->db->data($data)->save();

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        } else { //回显
            $id = I('id');
            $data = $this->db->where(array('id' => $id))->find();
            $this->ajaxReturn($data);
        }
    }

    //删除不是物理删除，只是状态的改变
    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $data['status'] = 0;
            $data['updatetime'] = time();
            $this->db->where(array('id' => $id))->setField($data);
            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }
}